<template>
  <div class="account-box">
    <Form ref="fm" :validation-schema="rules" v-slot="{ errors }" class="form">
      <!-- <p>{{ errors }}</p> -->
      <!-- 如果校验通过返回一个空，如果校验不通过，返回哪出错了 -->
      <div class="form-item">
        <div class="input">
          <i class="iconfont icon-user"></i>
          <Field
            v-model="formData.account"
            name="account"
            :class="{ error: errors.account }"
            type="text"
            placeholder="请输入用户名或手机号"
          />
        </div>
        <!-- 表单验证错误信息提示 -->
        <!-- <div class="error"><i class="iconfont icon-warning" />请输入手机号</div> -->
        <div v-if="errors.account" class="error">
          <i class="iconfont icon-warning" />{{ errors.account }}
        </div>
      </div>
      <div class="form-item">
        <div class="input">
          <i class="iconfont icon-lock"></i>
          <Field
            v-model="formData.password"
            name="password"
            :class="{ error: errors.password }"
            type="password"
            placeholder="请输入密码"
          />
        </div>
        <div v-if="errors.password" class="error">
          <i class="iconfont icon-warning" />{{ errors.password }}
        </div>
      </div>
      <div class="form-item">
        <div class="agree">
          <!-- <XtxCheckbox /> -->
          <Field as="XtxCheckbox" name="isAgree" v-model="formData.isAgree">
            <span>我已同意</span>
          </Field>
          <a href="javascript:;">《隐私条款》</a>
          <span>和</span>
          <a href="javascript:;">《服务条款》</a>
        </div>
        <div v-if="errors.isAgree" class="error">
          <i class="iconfont icon-warning" />{{ errors.isAgree }}
        </div>
      </div>
      <a @click="login" href="javascript:;" class="btn">登录</a>
    </Form>
    <div class="action">
      <img
        src="https://qzonestyle.gtimg.cn/qzone/vas/opensns/res/img/Connect_logo_7.png"
        alt=""
      />
      <div class="url">
        <a href="javascript:;">忘记密码</a>
        <a href="javascript:;">免费注册</a>
      </div>
    </div>
  </div>
</template>
<script>
import { Form, Field } from 'vee-validate'
import { reactive, ref } from 'vue'
import rulesfns from '@/utils/rules'
import { useStore } from 'vuex'
import Msg from '@/components/Message'
import { useRoute, useRouter } from 'vue-router'
export default {
  components: {
    Form, Field
  },
  setup () {
    const store = useStore()
    // 路由实例
    const router = useRouter()
    // 路由跳转参数
    const route = useRoute()
    // 4. 通过获取Form组件实例，调用实例方法validate()
    // 表单数据
    const formData = reactive({
      account: 'xiaotuxian001', // 用户名
      password: '123456', // 密码
      isAgree: false // 是否同意协议
    })
    // 校验规则
    const rules = {
      account: rulesfns.account,
      password: rulesfns.password,
      isAgree: rulesfns.isAgree
    }
    // 表单提交
    const fm = ref()
    const login = async () => {
      const { valid } = await fm.value.validate()
      if (valid) {
        // 调用接口
        try {
          // 如果用ref定义表单变量要加.value
          await store.dispatch('user/getProfile', formData)
          // 消息提示
          Msg({ type: 'success', text: '登陆成功' })
          // 跳转
          router.replace(route.query.redirectUrl || '/')
        } catch (error) {
          console.dir(error)
          // 消息提示
          Msg({ type: 'error', text: '登陆失败' })
        }
      }
    }
    return { formData, rules, login, fm }
  }
}
</script>
<style lang="less" scoped>
// 账号容器
.account-box {
  .toggle {
    padding: 15px 40px;
    text-align: right;
    a {
      color: @xtxColor;
      i {
        font-size: 14px;
      }
    }
  }
  .form {
    padding: 0 20px;
    &-item {
      margin-bottom: 28px;
      .input {
        position: relative;
        height: 36px;
        > i {
          width: 34px;
          height: 34px;
          background: #cfcdcd;
          color: #fff;
          position: absolute;
          left: 1px;
          top: 1px;
          text-align: center;
          line-height: 34px;
          font-size: 18px;
        }
        input {
          padding-left: 44px;
          border: 1px solid #cfcdcd;
          height: 36px;
          line-height: 36px;
          width: 100%;
          &.error {
            border-color: @priceColor;
          }
          &.active,
          &:focus {
            border-color: @xtxColor;
          }
        }
        .code {
          position: absolute;
          right: 1px;
          top: 1px;
          text-align: center;
          line-height: 34px;
          font-size: 14px;
          background: #f5f5f5;
          color: #666;
          width: 90px;
          height: 34px;
          cursor: pointer;
        }
      }
      > .error {
        position: absolute;
        font-size: 12px;
        line-height: 28px;
        color: @priceColor;
        i {
          font-size: 14px;
          margin-right: 2px;
        }
      }
    }
    .agree {
      a {
        color: #069;
      }
    }
    .btn {
      display: block;
      width: 100%;
      height: 40px;
      color: #fff;
      text-align: center;
      line-height: 40px;
      background: @xtxColor;
      &.disabled {
        background: #cfcdcd;
      }
    }
  }
  .action {
    padding: 20px 40px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .url {
      a {
        color: #999;
        margin-left: 10px;
      }
    }
  }
}
</style>
